//================================================================================================================================
// Code to log data to a file in the plugins directory
// 
//================================================================================================================================
                        
#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <time.h>
#include "MCE_dll_private.h"
#include "MCE.h"

static FILE *fp_log;


//================================================================================================================================
// Function to open the log file
//================================================================================================================================
extern int __stdcall Topen_log()
{  int old_debug, debug; 
   char str[80];

   old_debug=getbi(TAG_Debug);
  // If the log file fails to open there's not much we can do
   MCE_read_cfg_file();
   debug=getbi(TAG_Debug);
   if (geti(TAG_NewLogFile)==0) { strcpy(str,"a"); } else { strcpy(str,"w"); }
   if ( (debug!=0) && (old_debug==0) )                             // Switch it on
     { if ((fp_log=fopen("plugins\\MCE_dll.log",str))==NULL) { printf("Failed to open log file"); return 0; putbi(TAG_Debug,0); }
       else 
         { fprintf(fp_log,"***********************************************************\n");
           fflush(fp_log);
           sprintf(str," version %s",VER_STRING);
           Tlog(FILE_DESCRIPTION,str);
         }
     }
   else if ( (debug==0) && (old_debug!=0) ) { Tlog("open_log","log closed"); fclose(fp_log); }    // Switch it off
   MCE_read_cfg_file();    // read it twice so we can outot the log messages from it
}

//================================================================================================================================
// Function to close the log file
//================================================================================================================================
extern int __stdcall Tclose_log() { if (geti(TAG_Debug)!=0) fclose(fp_log); }                       // Close the log file

//================================================================================================================================
// Function to Log some text to the log file 
// in the form:
//     hh:mm:ss 23/07/11 <Module_Name> <Text>
//================================================================================================================================
extern int __stdcall Tlog(char * module, char * msg)
{
   time_t time_since;
   struct tm * cur_time;
   char log_time[20],log_line[120];
   
   if (getbi(TAG_Debug)!=0)
     { time( &time_since );   // get the time 
       cur_time =localtime( &time_since );  // convert to local time
       strftime(log_time, sizeof log_time, "%H:%M:%S %d/%m/%y", cur_time );  
       fprintf(fp_log,"%s %s %s\n",log_time,module,msg);
       printf("%s %s %s\n",log_time,module,msg);
       fflush(fp_log);
     }
}

//================================================================================================================================
// function to read from an configuration file
// 
//================================================================================================================================
extern void __stdcall MCE_read_cfg_file(void)
{
     FILE *fp;
     char line[128],str[80];
     int linelen,i,tag;

     fp=fopen("plugins\\MCE_dll.cfg","r");
     if (!fp) Tlog("read_cfg_file","Failed to open cfg file");
     Tlog("read_cfg_file","File Opened");
     if (fp != NULL) 
      { while (fgets(line,128,fp))
          {  strcpytonl(line,line);      // remove \n character
             linelen=strlen(line);
             if (linelen>0) 
              { if (line[0]!='/') 
               { if (!strncmp(line,"Tag=",4)) putdisplay(&line[4]);                
                 else { i=puttag(line, str, &tag); 
                        if (i==NUM_MTAGS) Tlog("read_cfg_file failed on tag",line);
                      }
               } 
              }
          }
        fclose(fp);
      }
    init_modes();     
}

